package com.xiaoxin.experience.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xiaoxin
 */
public class StringUtil
{
    private StringUtil(){}

    private static final char HYPHEN = '-';

    private static final Pattern HUMPPATTERN = Pattern.compile("[A-Z]");

    public static String toLowerCase(String str)
    {
        if (null == str)
        {
            return null;
        }
        return str.toLowerCase(Locale.getDefault());
    }

    public static String toUpperCase(String str)
    {
        if (null == str)
        {
            return null;
        }
        return str.toUpperCase(Locale.getDefault());
    }

    /**
     * 检查字符串是否合法，字符串为null,unknown，或者长度为0都认为非法
     */
    public static boolean validate(String str)
    {
        return validate(str,"null","unknown");
    }

    public static boolean validate(String str,String ... invalidArray)
    {
        if (isNotBlank(str))
        {
            if (invalidArray != null)
            {
                for (String invalid : invalidArray)
                {
                    if (invalid.equalsIgnoreCase(str))
                    {
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    }

    public static int length(final CharSequence cs)
    {
        return cs == null ? 0 : cs.length();
    }

    public static boolean isBlank(final CharSequence cs)
    {
        final int strLen = length(cs);
        if (strLen == 0)
        {
            return true;
        }
        for (int i = 0; i < strLen; i++)
        {
            if (!Character.isWhitespace(cs.charAt(i)))
            {
                return false;
            }
        }
        return true;
    }

    public static boolean isNotBlank(final CharSequence cs)
    {
        return !isBlank(cs);
    }

    /**
     * 字符串去掉前后空格
     */
    public static String trim(String arg)
    {
        if (null == arg)
        {
            return null;
        }
        else
        {
            return arg.trim();
        }
    }

    /**
     * 将null转为空字符串
     *
     * @param str 传入字符串
     */
    public static String toString(String str)
    {
        return null == str ? "" : str;
    }

    /**
     * 拼装字符串
     *
     * @param stringArr 需要拼接的字符串数组
     */
    public static String appendStr(String... stringArr)
    {
        if (ArrayUtil.isEmpty(stringArr))
        {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        for (String str : stringArr)
        {
            sb.append(toString(str));
        }
        return sb.toString();
    }



    public static String concat(Object... args)
    {
        if (ArrayUtil.isEmpty(args))
        {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (Object arg : args)
        {
            stringBuilder.append(arg);
        }

        return stringBuilder.toString();
    }


    public static int toInt(String str, int defValue)
    {
        try
        {
            return Integer.parseInt(str);
        }
        catch (NumberFormatException e)
        {
            return defValue;
        }
    }

    public static boolean equals(String left, String right)
    {
        return toString(left).equals(toString(right));
    }

    public static String floatFormat(double num)
    {
        return String.format("%.2f", num);
    }

    public static boolean isUrl(String str) {
        if (isBlank(str))
        {
            return false;
        }

        str = str.trim();
        return str.matches("^(http|https)://.+");
    }

    public static byte[] hexStringToByteArray(String s)
    {
        int len = s.length();
        if ((len & 1) == 1)
        {
            s = "0" + s;
            len++;
        }
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2)
        {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    public static String hyphenToCamel(String param)
    {
        if (StringUtils.isBlank(param))
        {
            return "";
        }

        if (!param.contains(String.valueOf(HYPHEN)))
        {
            return param;
        }

        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++)
        {
            char c = param.charAt(i);
            if (c == HYPHEN)
            {
                if (++i < len)
                {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            }
            else
            {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 驼峰格式字符串转换为中划线格式字符串
     */
    public static String camelToHyphen(String str)
    {
        Matcher matcher = HUMPPATTERN.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find())
        {
            matcher.appendReplacement(sb, HYPHEN + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String repeat(char c,int repeatCount)
    {
        char[] chars = new char[repeatCount];
        Arrays.fill(chars,c);
        return new String(chars);
    }
}
